<html ng-app="myApp">

	<head>
		<title>angular控制器传值实例</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js" type="text/javascript"></script>
	</head>
	<script type="text/javascript">
		var myApp = angular.module('myApp', []);
		myApp.controller('ParentController', function($scope) {
			//用于传递数据的变量
			$scope.data = 111;
		});

		myApp.controller('ChildOneController', function($scope, instance) {
			//数据设置
			$scope.data = 222;

			$scope.showNum = function() {
				console.log("父控制器定义的值：" + $scope.$parent.data);
				console.log("子控制器定义的值：" + $scope.data);

				console.log(instance);
			}
		});

		myApp.controller('ChildTwoController', function($scope, $timeout) {
			$timeout(function() {
				//console.log($scope);
				console.log($scope.data);
				//console.log($scope.$parent);
				console.log($scope.$parent.data);
			}, 1000);
		});

		/*
		 * 通过服务（service）两个控制器之间的传值
		 */
		myApp.service('instance', function() {
			return {};
		});

		myApp.controller('OneController', function($scope, instance) {
			$scope.setValue = function() {
				instance.name = $scope.Student.name;
				instance.age = $scope.Student.age;
			}
		});

		myApp.controller('TwoController', function($scope, instance) {
			$scope.getValue = function() {
				console.log(instance);
				$scope.name = instance.name;
				$scope.age = instance.age;
			}
		});
	</script>

	<body ng-controller="ParentController">
		<!--
        	作者：mengweiwang@163.com
        	时间：2017-03-29
        	描述：父控制器与子控制器之间的传值
        -->
		<div ng-controller="ChildOneController" ng-click="showNum()">点我显示</div>
		<div ng-controller="ChildTwoController"></div>
		<!--
        	作者：mengweiwang@163.com
        	时间：2017-03-29
        	描述：通过服务（service）两个控制器之间的传值
        -->
		<div ng-controller="OneController">
			<input type="text" ng-model="Student.name" />
			<input type="text" ng-model="Student.age" />
			<input type="button" ng-click="setValue()" value="设置" />
		</div>
		<div ng-controller="TwoController" ng-click="getValue()">
			取值：<br /> 姓名{{name}}
			<br /> 年龄{{age}}
		</div>
	</body>

</html>